package com.twu.biblioteca;

import java.io.BufferedReader;
import java.io.Console;
import java.io.IOException;
import java.io.InputStreamReader;

public class BibliotecaApp {

    private static User currentUser = null;

    public static void main(String[] args) {
        System.out.println("Welcome, biblioteca user!");
        mainMenu();
    }

    private static void mainMenu() {
        printMainMenu();

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        while(true) {
            System.out.print("Enter Your Choice: ");
            try{
                int i = Integer.parseInt(br.readLine());
                if(i == 1) {
                    if(currentUser == null) {
                        login();
                        printMainMenu();
                    } else {
                        System.out.print(currentUser.toString());
                    }
                } else if(i == 2) {
                    listMaterials();
                    printMainMenu();
                } else if(i == 3 || i == 4) {
                    if(currentUser != null) {
                        if(i == 3)
                            checkoutMaterial();
                        else
                            returnMaterial();
                        printMainMenu();
                    } else {
                        System.out.println("This feature requires user login.");
                    }
                } else if(i == 5) {
                    System.exit(0);
                } else {
                    System.out.println("Select a valid option!");
                }

            } catch (NumberFormatException nfe){
                System.out.println("Select a valid option!");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    private static void printMainMenu() {
        System.out.println();
        System.out.println("Main Menu");
        System.out.println();
        if(currentUser == null)
            System.out.println("1. Login");
        else
            System.out.println("1. User Info");
        System.out.println("2. List Books/Movies");
        System.out.println("3. Check Out A Book/Movie");
        System.out.println("4. Return A Book/Movie");
        System.out.println("5. Quit");
    }

    private static boolean login() {

        while(true) {
            try{
                Console console = System.console();
                if (console == null) {
                    System.out.println("ERROR: Couldn't get console instance, " +
                            "please run this program on a stand-alone terminal");
                    System.exit(0);
                }
                String username = console.readLine("Username(test user: twu): ");
                String password = new String(console.readPassword("Password(test password: twu): "));

                currentUser = Library.INSTANCE.authenticate(username, password);
                if(currentUser != null) {
                    return true;
                } else {
                    System.out.println("Incorrect credential. Try Again.");
                }

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    private static void returnMaterial() {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        while(true) {
            System.out.print("Enter the book/movie ID to be returned, or 0 to return to main menu: ");
            try{
                int i = Integer.parseInt(br.readLine());
                if(i == 0) {
                    return;
                } else if(i < 0) {
                    throw new NumberFormatException("expected non-negative integer");
                } else {
                    currentUser.returnMaterial(i);
                    System.out.println("Thank you for returning this item.");
                    return;
                }

            } catch (NumberFormatException nfe){
                System.out.println("Please input a valid item ID, or type 0 to return to main menu!");
            } catch (MaterialReturnException be) {
                System.out.println("That is not a valid book/movie to return, " + be.getMessage());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    private static void checkoutMaterial() {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        while(true) {
            System.out.print("Enter the book/movie ID to be borrowed, or 0 to return to main menu: ");
            try{
                int i = Integer.parseInt(br.readLine());
                if(i == 0) {
                    return;
                } else if(i < 0) {
                    throw new NumberFormatException("expected non-negative integer");
                } else {
                    Material material = currentUser.checkoutMaterial(i);
                    System.out.println("Thank you! Enjoy the book/movie \"" + material.getName() +"\"");
                    return;
                }

            } catch (NumberFormatException nfe){
                System.out.println("Please input a valid item ID, or type 0 to return to main menu!");
            } catch (MaterialCheckOutException be) {
                System.out.println("That item is not available, " + be.getMessage());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    private static void listMaterials() {

        String allBooks = Library.INSTANCE.listAllAvailableBooks();
        if(allBooks.isEmpty())
            allBooks = "No book currently available. Come back later.\n";
        String allMovies = Library.INSTANCE.listAllAvailableMovies();
        if(allMovies.isEmpty())
            allMovies = "No movie currently available. Come back later.\n";

        System.out.println();
        System.out.println("------------Books-----------");
        System.out.println("ID|Name|Author|Publish Year ");
        System.out.println("----------------------------");
        System.out.print(allBooks);
        System.out.println();
        System.out.println("-----------Movies-----------");
        System.out.println("ID|Name|Year|Director|Rating");
        System.out.println("----------------------------");
        System.out.print(allMovies);
        System.out.println();
    }
}
